package com.gton.lpl.controller;


import com.gton.lpl.common.annotation.NeedTokenByJWT;
import com.gton.lpl.common.pojo.Resp;
import com.gton.lpl.entity.GpMenu;
import com.gton.lpl.service.GpMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * <p>
 * //菜单配置
 * 　1) SpringBoot项目启动,  spring.factories 引入了 SessionAutoConfiguration ,
 *      导致 RedisHttpSessionConfiguration、SpringHttpSessionConfiguration 两个配置类自动导入
 * 　　　如果是Spring项目,就需要使用注解 @EnableRedisHttpSession 来开启RedisHttpSession
 * <p>
 * 　　2) RedisHttpSessionConfiguration 主要是配置了 RedisIndexedSessionRepository Session仓库类
 * <p>
 * 　　3) SpringHttpSessionConfiguration 主要是配置了 SessionRepositoryFilter Session仓库过滤器
 * <p>
 * 　　4) 通过 SessionRepositoryFilter 过滤器对请求进行拦截, 进行逻辑判断, 将 session 保持到 redis中
 * </p>
 *
 * @author GuoTong
 * @since 2021-10-17
 */
@RestController
@RequestMapping("/gp-menu")
@Api(tags = {"首页菜单控制台配置"})
public class GpMenuController {

    @Autowired
    private GpMenuService gpMenuService;


    @GetMapping(value = "/getMenuById", name = "根据ID获取菜单详细信息")
    @NeedTokenByJWT
    @ApiOperation("根据ID获取菜单详细信息")
    public Resp getMenuById(@RequestParam("menuId") String menuId, HttpServletRequest request, HttpServletResponse response) {
        //判断菜单是否合法
        if (!menuId.matches("1[0-9]\\d{17}")) {
            return Resp.error("参数传入有误");
        }
        request.getSession(false).setAttribute("menuId", menuId);

        GpMenu menu = gpMenuService.getById(menuId);
        return Resp.Ok(menu);

    }

    @GetMapping(value = "/getSession", name = "getSessionByRedis")
    @NeedTokenByJWT
    @ApiOperation("getSessionByRedis")
    public Resp getSession(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        return Resp.Ok(session.getAttribute("menuId"));
    }
}

